package me.zhengjie.modules.region.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import me.zhengjie.base.PageInfo;
import me.zhengjie.base.QueryHelpMybatisPlus;

import me.zhengjie.base.impl.CommonServiceImpl;
import me.zhengjie.modules.region.domain.vo.AreaRegionVo;
import me.zhengjie.modules.region.domain.vo.CityRegionVo;
import me.zhengjie.modules.region.domain.vo.ProvinceRegionVo;
import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.modules.region.domain.SysRegion;
import me.zhengjie.modules.region.service.SysRegionService;
import me.zhengjie.modules.region.service.dto.SysRegionDto;
import me.zhengjie.modules.region.service.dto.SysRegionQueryParam;
import me.zhengjie.modules.region.service.mapper.SysRegionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author TianI
* @date 2022-01-27
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = SysRegionService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SysRegionServiceImpl extends CommonServiceImpl<SysRegionMapper, SysRegion> implements SysRegionService {

    // private final RedisUtils redisUtils;
    private final SysRegionMapper sysRegionMapper;

    @Override
    public PageInfo<SysRegionDto> queryAll(SysRegionQueryParam query, Pageable pageable) {
        IPage<SysRegion> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<SysRegion> page = sysRegionMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, SysRegionDto.class);
    }

    @Override
    public List<SysRegionDto> queryAll(SysRegionQueryParam query){
        return ConvertUtil.convertList(sysRegionMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), SysRegionDto.class);
    }

    @Override
    public SysRegion getById(String id) {
        return sysRegionMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public SysRegionDto findById(String id) {
        return ConvertUtil.convert(getById(id), SysRegionDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(SysRegionDto resources) {
        SysRegion entity = ConvertUtil.convert(resources, SysRegion.class);
        return sysRegionMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(SysRegionDto resources){
        SysRegion entity = ConvertUtil.convert(resources, SysRegion.class);
        int ret = sysRegionMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<String> ids){
        // delCaches(ids);
        return sysRegionMapper.deleteBatchIds(ids);
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(String id){
        Set<String> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    @Override
    public List<ProvinceRegionVo> getregion(String parentId) {
        //查询省级目录
        QueryWrapper<SysRegion> provincewrapper=new QueryWrapper<>();
        provincewrapper.eq("ParentId",parentId);
        List<SysRegion> provinceList=baseMapper.selectList(provincewrapper);

        List<ProvinceRegionVo> provinceListvo=new ArrayList<>();
        //封装省级目录
        for(int i=0; i<provinceList.size(); i++){
            SysRegion provinceRegion =provinceList.get(i);
            ProvinceRegionVo province=new ProvinceRegionVo();
            BeanUtils.copyProperties(provinceRegion,province);
            provinceListvo.add(province);

            //查询市级目录
            QueryWrapper<SysRegion> citywrapper=new QueryWrapper<>();
            citywrapper.eq("ParentId",provinceRegion.getValue());
            List<SysRegion> cityList=baseMapper.selectList(citywrapper);

            List<CityRegionVo> CityRegionVo=new ArrayList<>();
            //封装市级目录
            for(int j=0;j<cityList.size();j++){
                SysRegion cityRegion =cityList.get(j);
                CityRegionVo city=new CityRegionVo();
                BeanUtils.copyProperties(cityRegion,city);
                CityRegionVo.add(city);

                //查询区级目录
                QueryWrapper<SysRegion> areawrapper =new QueryWrapper<>();
                areawrapper.eq("ParentId",cityRegion.getValue());
                List<SysRegion> areaList=baseMapper.selectList(areawrapper);

                List<AreaRegionVo> AreaRegionVo=new ArrayList<>();
                //封装区级目录
                for (int k=0;k<areaList.size();k++){
                    SysRegion areaRegion =areaList.get(k);
                    AreaRegionVo area=new AreaRegionVo();
                    BeanUtils.copyProperties(areaRegion,area);
                    AreaRegionVo.add(area);
                }
                city.setChildren(AreaRegionVo);
            }
            province.setChildren(CityRegionVo);
        }

        return provinceListvo;
    }
    /*
    @Override
    public void download(List<SysRegionDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (SysRegionDto sysRegion : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("名称", sysRegion.getName());
              map.put("父id", sysRegion.getParentid());
              map.put("简称", sysRegion.getShortname());
              map.put("级别", sysRegion.getLeveltype());
              map.put("城市区号", sysRegion.getCitycode());
              map.put("邮编", sysRegion.getZipcode());
              map.put("全名", sysRegion.getMergername());
              map.put("经度", sysRegion.getLng());
              map.put("纬度", sysRegion.getLat());
              map.put("拼音", sysRegion.getPinyin());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
